package com.mall.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.mall.entity.CodeNumEntity;
import com.mall.entity.ResponseMessage;
import com.mall.service.GoodsService;
import com.mall.service.impl.GoodsServiceImpl;
import com.mall.util.IdGenerator;
import com.mall.util.PageUtils;
import com.mall.entity.Goods;
import com.mall.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @author:lck
 * @Date 2024/1/15 15:42
 */

@RestController
@RequestMapping("/goods")
public class GoodsController {
    private GoodsService goodsService;
    @Autowired
    public GoodsController(GoodsService goodsService){
        this.goodsService = goodsService;
    }

    //管理员点击 商品管理 获取商品列表信息
//    goods/1/10
    @GetMapping("/{currentPage}/{pageSize}")
    public ResponseMessage getGoodsList(@PathVariable Integer currentPage, @PathVariable Integer pageSize){
        PageUtils<Goods> goodsList = goodsService.getGoodsList(currentPage, pageSize);

        Map<String, Object> map = new HashMap<>();
        map.put("message", goodsList);

        return new ResponseMessage(CodeNumEntity.SUCCESS.getCode(), CodeNumEntity.SUCCESS.getMessage(), map);
    }



    //添加图书，用Goods对象接收传入的参数
    @PostMapping("/good")
    public ResponseMessage addGoods(@RequestBody Goods good){
        if(good == null)
            return new ResponseMessage(CodeNumEntity.FAIL.getCode(), "未传入数据");
        System.out.println(good);
        int i = goodsService.addGoods(good);
        if(i > 0){
            return new ResponseMessage(CodeNumEntity.SUCCESS.getCode(), CodeNumEntity.SUCCESS.getMessage());
        }

        return new ResponseMessage(CodeNumEntity.DATABASE_ERROR.getCode(), CodeNumEntity.DATABASE_ERROR.getMessage());
    }



    //按照ID好查询，其实可以没有
    @GetMapping("/{goods_id}")
    public ResponseMessage getGoodsById(@PathVariable String goods_id){
        if(goods_id == null)
            return new ResponseMessage(CodeNumEntity.FAIL.getCode(), "未传入数据");
        System.out.println(goods_id);
        Goods good = goodsService.getGoodsById(goods_id);
        System.out.println(good);
        if(good == null){
            return new ResponseMessage(CodeNumEntity.DATABASE_ERROR.getCode(), CodeNumEntity.DATABASE_ERROR.getMessage());
        }
        return new ResponseMessage(CodeNumEntity.SUCCESS.getCode(), CodeNumEntity.SUCCESS.getMessage());
    }

    //模糊查询
    @GetMapping("/{keyword}/{currentPage}/{pageSize}")
    public ResponseMessage getGoodsByLike(@PathVariable String keyword, @PathVariable Integer currentPage, @PathVariable Integer pageSize){
        PageUtils<Goods> goods = goodsService.getGoodsByLike(keyword, currentPage, pageSize);
        Map<String, Object> map = new HashMap<>();

        map.put("message", goods);
        return new ResponseMessage(CodeNumEntity.SUCCESS.getCode(), CodeNumEntity.SUCCESS.getMessage(), map);
    }



    //商品下架功能
    @PostMapping("/goods/delistGoods")
    public ResponseMessage delistGoods(@RequestParam ArrayList<String> goodsId){
        //获取列表中所有已经下架的图书的数量
        int delistNum = goodsService.getDelistNum(goodsId);
        //将列表中的图书均下架，获取受到影响的数据的数量
        int count = goodsService.delistGoods(goodsId);

        ResponseMessage responseMessage = new ResponseMessage();
        if (count == (goodsId.size() - delistNum)){
            Map<String, Object> content = new HashMap<>();
            content.put("message", "商品下架成功");
            responseMessage.setStatusCode(CodeNumEntity.SUCCESS.getCode());
            responseMessage.setStatusMessage(CodeNumEntity.SUCCESS.getMessage());
            responseMessage.setContent(content);
        }else if (count != (goodsId.size() - delistNum)){
            responseMessage.setStatusCode(CodeNumEntity.FAIL.getCode());
            responseMessage.setStatusMessage(CodeNumEntity.FAIL.getMessage());
        }else {
            responseMessage.setStatusCode(CodeNumEntity.SERVER_ERROR.getCode());
            responseMessage.setStatusMessage(CodeNumEntity.SERVER_ERROR.getMessage());
        }

        return responseMessage;
    }

    //商品上架功能
    @PostMapping("/goods/listGoods")
    public ResponseMessage listGoods(@RequestParam ArrayList<String> goodsId){
        //获取列表中所有已经下架的图书的数量
        int delistNum = goodsService.getDelistNum(goodsId);
        //将列表中的图书均下架，获取受到影响的数据的数量
        int count = goodsService.listGoods(goodsId);

        ResponseMessage responseMessage = new ResponseMessage();
        if(count == delistNum){
            Map<String, Object> content = new HashMap<>();
            content.put("message", "商品上架成功");
            responseMessage.setStatusCode(CodeNumEntity.SUCCESS.getCode());
            responseMessage.setStatusMessage(CodeNumEntity.SUCCESS.getMessage());
            responseMessage.setContent(content);
        }else if (count != delistNum){
            responseMessage.setStatusCode(CodeNumEntity.FAIL.getCode());
            responseMessage.setStatusMessage(CodeNumEntity.FAIL.getMessage());
        }else {
            responseMessage.setStatusCode(CodeNumEntity.SERVER_ERROR.getCode());
            responseMessage.setStatusMessage(CodeNumEntity.SERVER_ERROR.getMessage());
        }

        return responseMessage;
    }

}
